<?php
/*
	[BuyPg] (C) 2010-2011 Buypg
	$Id: TousuAction.class.php 2011-5-13 9:44:09 wufujun $
*/
if (!defined('THINK_PATH')) {
	exit('Access Denied');
}

class TousuAction extends BpAction{
	public function index(){
		global $_SGLOBAL;
		$nbcid = isset($_GET['nbcid']) ? intval($_GET['nbcid']) : 0;
		$siteid = isset($_GET['siteid']) ? intval($_GET['siteid']) : 0;
		if($nbcid){
			$andwhere = " and NbClass.class_id = $nbcid";
		}elseif($siteid){
			$andwhere = " and NbTousu.site_id = $siteid";
		}else{
			$andwhere = "";
		}
		//通过视图查询
		$DbVTouSu = D("TousuView");
		import("@.ORG.Page"); //导入分页类
		$count = $DbVTouSu->where("NbTousu.flag = 1 $andwhere")->count();    //计算总数
		$p = new Page ( $count, 20 );
		$arrTouSu = $DbVTouSu->where("NbTousu.flag = 1 $andwhere")->limit($p->firstRow.','.$p->listRows)->order("NbSite.country_id asc,NbTousu.tousu_dt desc")->select();
		$p->setConfig('header','篇记录');
		$p->setConfig('prev','<');
		$p->setConfig('next','>');
		$p->setConfig('first','<<');
		$p->setConfig('last','>>');
		$p->setConfig('theme', ' <span>%totalRow% %header% %nowPage%/%totalPage% 页 </span> %first% %upPage% %linkPage% %downPage% %end%');
		$page = $p->show(1);
		//格式化数据
		foreach ($arrTouSu as $key => $value){
			$arrTouSu[$key]['site_link'] = urldecode($value['site_link']);
		}
		//获取分类
		$arrCate = getCategory();
		//模板输出
		$this->assign('show_zhanti_menu' , 1); //左侧菜单显示专题
		$this->assign('arrCate' , $arrCate);
		$this->assign('arrTouSu' , $arrTouSu);
		$this->assign('page' , $page);
		$this->display();
	}
	
	public function showtousu(){
		global $_SGLOBAL;
		$tousuid = isset($_GET['tousuid']) ? intval($_GET['tousuid']) : 0;
		$DbTouSu = M("NbTousu");
		$DbGaiShan = M("NbGaishan");
		$DbSite = M("NbSite");
		//$arrTouSu = $DbTouSu->where("tousu_id = $tousuid")->find();
		$arrTouSu = $DbTouSu->query("SELECT ts.* , c.class_name FROM `t_nb_tousu` ts LEFT JOIN `t_tousu_class` c ON ts.class_id = c.class_id WHERE ts.tousu_id = $tousuid ");
		if(!$arrTouSu){
			$this->error('您查看的信息不存在或已经删除');
		}
		$arrGaiShan = $DbGaiShan->where("tousu_id = $tousuid")->find();
		$arrSite = $DbSite->where("site_id = ".$arrTouSu[0]['site_id'])->find();
		
		//相关投诉
		$arrAboutTs = $DbTouSu->where("site_id = ".$arrSite['site_id']." and tousu_id not in(".$arrTouSu[0]['tousu_id'].")")->select();
		//相关改善
		$arrAboutGs = $DbGaiShan->where("site_id = ".$arrSite['site_id']." and tousu_id not in(".$arrTouSu[0]['tousu_id'].")")->select();
		//获取分类
		$arrCate = getCategory();
		//模板输出
		$this->assign('show_zhanti_menu' , 1); //左侧菜单显示专题
		$this->assign('arrCate' , $arrCate);
		$this->assign('arrTouSu' , $arrTouSu[0]);
		$this->assign('arrGaiShan' , $arrGaiShan);
		$this->assign('arrAboutTs' , $arrAboutTs);
		$this->assign('arrAboutGs' , $arrAboutGs);
		$this->assign('arrSite' , $arrSite);
		$this->display();
	}
	
	public function tousu(){
		global $_SGLOBAL;
		$siteid = isset ($_GET['siteid']) ? intval($_GET['siteid']) : 0;
		//判断用户
		if(($mserr = checkstatus('user'))){
			if($mserr['flag'] == 1){
				$this->assign('jumpUrl' , __APP__."/User/login");
			}
			$this->error($mserr['msg']);
		}
		if($siteid){
			$DbSite = M("NbSite");
			$DbTouSuClass = M("TousuClass");
			$arrTouSuClass = $DbTouSuClass->select();
			$arrSite = $DbSite->where("site_id = $siteid and flag = 1")->find();
			if(!$arrSite){
				$this->error('您查看的信息不存在或已经删除');
			}
		}else{
			$this->error('您查看的信息不存在或已经删除');
		}
		
		//模板输出
		$this->assign('arrTouSuClass' , $arrTouSuClass);
		$this->assign('arrSite' , $arrSite);
		$this->display();
	}
	
	public function insert(){
		global $_SGLOBAL;
		//判断用户
		if(($mserr = checkstatus('user'))){
			$this->error($mserr['msg']);
		}
		$DbTouSu = D("NbTousu");
		if($DbTouSu->create()){
			if(false !== $DbTouSu->add()) {
                $this->assign('jumpUrl', U('BuyPg://Webping/website' , array('siteid'=>$_POST['site_id'])));
				$this->success("您已经成功提交投诉，请耐心等待审核");
            }else{
                $this->error('投诉提交失败');
            } 
		}else{
			$this->assign('jumpUrl',U('BuyPg://Tousu/tousu', array('siteid'=>$_POST['site_id'])));
			$this->error($DbTouSu->getError());
		}
	}
}
?>
